package com.tuxing.draa_data.pojo.center;

import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.annotation.TableId;

import java.time.LocalDateTime;

import com.baomidou.mybatisplus.annotation.TableField;

import java.io.Serializable;

import com.google.common.base.Strings;
import com.tuxing.draa_data.common.DraaContent;
import com.tuxing.draa_data.pojo.ChaoXingPeri;
import com.tuxing.draa_data.pojo.DraaTemporaryBook;
import com.tuxing.draa_data.pojo.db.*;
import com.tuxing.draa_data.util.ConvertUtil;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.StringUtils;

/**
 * <p>
 * 期刊实例表
 * </p>
 *
 * @author yuanshihang
 * @since 2022-03-31
 */
@Data
@EqualsAndHashCode(callSuper = false)
@TableName("new_dc_peri")
public class NewDcPeri implements Serializable {

    private static final long serialVersionUID = 1L;

    @TableId("TITLE_ID")
    private Long titleId;

    /**
     * 库id
     */
    @TableField("COLLECTION_ID")
    private Long collectionId;

    /**
     * 包id
     */
    @TableField("PACKAGE_ID")
    private Long packageId;

    /**
     * 期刊元数据记录号
     */
    @TableField("PERI_RECORD_ID")
    private Long periRecordId;

    /**
     * 年份
     */
    @TableField("YEAR")
    private Integer year;

    /**
     * 覆盖范围开始时间
     */
    @TableField("SCOPE_BEGIN")
    private String scopeBegin;

    /**
     * 覆盖范围结束时间
     */
    @TableField("SCOPE_END")
    private String scopeEnd;

    /**
     * 时间覆盖范围
     */
    @TableField("TIME_SCOPE")
    private String timeScope;

    /**
     * URL
     */
    @TableField("URL")
    private String url;

    /**
     * 1=外文期刊，3=中文期刊（1=外文，3=中文）
     */
    @TableField("DATA_TYPE")
    private Integer dataType;

    /**
     * 1=OA刊，0=非OA刊
     */
    @TableField("OA")
    private Integer oa;

    /**
     * 创建人
     */
    @TableField("CREATE_BY")
    private Integer createBy;

    /**
     * 创建时间
     */
    @TableField("CREATE_DATE")
    private LocalDateTime createDate;

    /**
     * 更新人
     */
    @TableField("UPDATE_BY")
    private Integer updateBy;

    /**
     * 更新时间
     */
    @TableField("UPDATE_DATE")
    private LocalDateTime updateDate;

    /**
     * 1=draa库包关系 2=超星资源库包关系
     */
    @TableField("SOURCE_TYPE")
    private Integer sourceType;


//    /**
//     * 0=中文 1=外文
//     */
//    @TableField("CHOREN")
//    private Integer choren;
//
//    /**
//     * 期刊名称
//     */
//    @TableField("MAG_NAME")
//    private String magName;
//
//    /**
//     * ISSN号
//     */
//    @TableField("ISSN")
//    private String issn;
//
//    /**
//     * 主板单位
//     */
//    @TableField("UNIT")
//    private String unit;
//
//    /**
//     * 周期
//     */
//    @TableField("CYCLE")
//    private String cycle;
//
//    /**
//     * 影响因子
//     */
//    @TableField("IF_VALUE")
//    private String ifValue;
//
//    /**
//     * 重要期刊收录
//     */
//    @TableField("ORIGN_RECORD")
//    private String orignRecord;
//
//    /**
//     * 摘要
//     */
//    @TableField("SUMMARY")
//    private String summary;
//
//    /**
//     * 所属学科
//     */
//    @TableField("SUBJECT_NAME")
//    private String subjectName;
//
//    @TableField("CN")
//    private String cn;

    @TableField("MAG_ID")
    private String magId;

    @TableField("DELAY")
    private Integer delay;

    public void createByCnki(Long collectionId, Long packageId, Long periRecordId, Integer year, String scopeBegin, String scopeEnd, String timeScope, String url, Integer dataType, Integer oa, Integer delay, Integer sourceType) {
        this.delay = delay;
        this.collectionId = collectionId;
        this.packageId = packageId;
        this.periRecordId = periRecordId;
        this.year = year;
        this.scopeBegin = scopeBegin;
        this.scopeEnd = scopeEnd;
        this.timeScope = timeScope;
        this.url = url;
        this.dataType = dataType;
        this.oa = oa;
        this.sourceType = sourceType;
    }

    public void create(Long journalRecordId, Integer dataType, NewDcPackage newDcPackage, Integer year, DraaTemporaryBook temporaryJournal) {
        this.collectionId = newDcPackage.getCollectionId();
        this.packageId = newDcPackage.getPackageId();
        this.periRecordId = journalRecordId;
        this.year = year;
        this.dataType = dataType;
        this.scopeBegin = temporaryJournal.getRangeStart();
        this.scopeEnd = temporaryJournal.getRangeEnd();
        this.timeScope = temporaryJournal.getIncludeRange();
        this.delay = ConvertUtil.objToInt(temporaryJournal.getEmbargoDays(), 0);
        this.sourceType = 1;
        this.oa = ConvertUtil.objToInt(temporaryJournal.getFree(), 0);
        this.url = temporaryJournal.getCompanyUrl();
        this.createBy = -1;
        this.createDate = LocalDateTime.now();
        this.updateBy = -1;
        this.updateDate = LocalDateTime.now();
    }

    public void createByCx(ChaoXingPeri peris, Long packageId) {
//        this.titleId = titleId;
        this.collectionId = ConvertUtil.objToLong(peris.getResourceId());
        this.packageId = packageId;
        this.periRecordId = ConvertUtil.objToLong(peris.getMagId());
//        this.year = year;
//        this.scopeBegin = scopeBegin;
//        this.scopeEnd = scopeEnd;
        this.timeScope = peris.getRecodeRange();
//        this.url = url;
//        this.dataType = dataType;
//        this.oa = oa;
        this.createBy = -1;
        this.createDate = LocalDateTime.now();
        this.updateBy = -1;
        this.updateDate = LocalDateTime.now();
        this.sourceType = DraaContent.OrderSource.FROM_CHAOXING;
//        this.choren = peris.getChoren();
//        this.magName = peris.getMagName();
//        this.issn = peris.getIssn();
//        this.unit = peris.getUnit();
//        this.cycle = peris.getCycle();
//        this.ifValue = peris.getInfluenceFactor();
//        this.orignRecord = peris.getOrignRecord();
//        this.summary = peris.getSummary();
//        this.subjectName = peris.getSubjectName();
//        this.cn = peris.getCn();
    }


    public void createByWeiPu(Weipu20220629 weipu20220629, Integer year, Long recordId) {
        this.collectionId = 1466399815248834563L;
        this.packageId = 1509459078363656198L;
        this.periRecordId = recordId;
        this.sourceType = DraaContent.OrderSource.FROM_DRAA;
//        this.issn = weipu20220629.getIssn();
//        this.cn = weipu20220629.getCn();
        String years = weipu20220629.getYear();
        if (StringUtils.isNotBlank(years)) {
            String[] split = years.split(";");
            if (split.length > 0) {
                this.scopeEnd = split[0];
                this.scopeBegin = split[split.length - 1];
                this.timeScope = StringUtils.join(split[split.length - 1], "-", split[0]);
            }
        }
        this.url = weipu20220629.getPeriUrl();
        this.year = year;
        this.oa = 0;
        this.dataType = 3;

        this.createBy = -1;
        this.createDate = LocalDateTime.now();
        this.updateBy = -1;
        this.updateDate = LocalDateTime.now();

    }

    public void createByAdis(Adis20220630 adis20220630, Integer year, Long recordId) {
        this.collectionId = 1466399814976204806L;
        this.packageId = 1534881876774948870L;
        this.periRecordId = recordId;
        this.sourceType = DraaContent.OrderSource.FROM_DRAA;
        this.url = adis20220630.getUrl();
        this.year = year;
        this.oa = 0;
        this.dataType = 1;

        this.createBy = -1;
        this.createDate = LocalDateTime.now();
        this.updateBy = -1;
        this.updateDate = LocalDateTime.now();

    }

    public void createByAdis2(Adis20220630001 adis20220630001, Integer year, Long recordId) {
        this.collectionId = 1466399814976204806L;
        this.packageId = 1534881876774948870L;
        this.periRecordId = recordId;
        this.sourceType = DraaContent.OrderSource.FROM_DRAA;
        this.year = year;
        this.oa = 0;
        this.dataType = 1;
        this.scopeBegin = adis20220630001.getFirstYear();
        this.scopeEnd = adis20220630001.getLastYear();
        this.timeScope = StringUtils.join(adis20220630001.getFirstYear(), "-", adis20220630001.getLastYear());

        this.createBy = -1;
        this.createDate = LocalDateTime.now();
        this.updateBy = -1;
        this.updateDate = LocalDateTime.now();

    }

    public void createByArt(Art20220701 art20220701, Integer year, Long recordId) {
        this.collectionId = 1466399815177531400L;
        this.packageId = 1534881876774948871L;
        this.periRecordId = recordId;
        this.sourceType = DraaContent.OrderSource.FROM_DRAA;
        this.year = year;
        this.oa = 0;
        this.dataType = 1;
        this.scopeBegin = art20220701.getIndexingandAbstractingStart().substring(0, 4);
        if (StringUtils.isBlank(art20220701.getIndexingandAbstractingStop())) {
            this.timeScope = this.scopeBegin + "至今";
        } else {
            this.scopeEnd = art20220701.getIndexingandAbstractingStop().substring(0, 4);
            this.timeScope = this.scopeBegin + "-" + this.scopeEnd;
        }

        this.createBy = -1;
        this.createDate = LocalDateTime.now();
        this.updateBy = -1;
        this.updateDate = LocalDateTime.now();

    }

    public void createByBsc(Bsc20220701 bsc20220701, Integer year, Long recordId) {
        this.collectionId = 1466399815177531396L;
        this.packageId = 1534430380178513927L;
        this.periRecordId = recordId;
        this.sourceType = DraaContent.OrderSource.FROM_DRAA;
        this.year = year;
        this.oa = 0;
        this.dataType = 1;
        this.scopeBegin = bsc20220701.getIndexingandAbstractingStart().substring(0, 4);
        if (StringUtils.isBlank(bsc20220701.getIndexingandAbstractingStop())) {
            this.timeScope = this.scopeBegin + "至今";
        } else {
            this.scopeEnd = bsc20220701.getIndexingandAbstractingStop().substring(0, 4);
            this.timeScope = this.scopeBegin + "-" + this.scopeEnd;
        }

        this.createBy = -1;
        this.createDate = LocalDateTime.now();
        this.updateBy = -1;
        this.updateDate = LocalDateTime.now();

    }

    public void createByBsp(Bsp20220701 bsp20220701, Integer year, Long recordId) {
        this.collectionId = 1466399815177531397L;
        this.packageId = 1534430380430172162L;
        this.periRecordId = recordId;
        this.sourceType = DraaContent.OrderSource.FROM_DRAA;
        this.year = year;
        this.oa = 0;
        this.dataType = 1;
        this.scopeBegin = bsp20220701.getIndexingandAbstractingStart().substring(0, 4);
        if (StringUtils.isBlank(bsp20220701.getIndexingandAbstractingStop())) {
            this.timeScope = this.scopeBegin + "至今";
        } else {
            this.scopeEnd = bsp20220701.getIndexingandAbstractingStop().substring(0, 4);
            this.timeScope = this.scopeBegin + "-" + this.scopeEnd;
        }

        this.createBy = -1;
        this.createDate = LocalDateTime.now();
        this.updateBy = -1;
        this.updateDate = LocalDateTime.now();

    }

    public void createByBsu(Bsu20220701 bsu20220701, int year, Long recordId) {
        this.collectionId = 1466399814640660486L;
        this.packageId = 1534430382627987462L;
        this.periRecordId = recordId;
        this.sourceType = DraaContent.OrderSource.FROM_DRAA;
        this.year = year;
        this.oa = 0;
        this.dataType = 1;
        this.scopeBegin = bsu20220701.getIndexingandAbstractingStart().substring(0, 4);
        if (StringUtils.isBlank(bsu20220701.getIndexingandAbstractingStop())) {
            this.timeScope = this.scopeBegin + "至今";
        } else {
            this.scopeEnd = bsu20220701.getIndexingandAbstractingStop().substring(0, 4);
            this.timeScope = this.scopeBegin + "-" + this.scopeEnd;
        }

        this.createBy = -1;
        this.createDate = LocalDateTime.now();
        this.updateBy = -1;
        this.updateDate = LocalDateTime.now();
    }

    public void createByCinahk(Cinahk20220701 cinahk20220701, int year, Long recordId) {
        this.collectionId = 1466399814573551620L;
        this.packageId = 1468441523532808194L;
        this.periRecordId = recordId;
        this.sourceType = DraaContent.OrderSource.FROM_DRAA;
        this.year = year;
        this.oa = 0;
        this.dataType = 1;
        this.scopeBegin = cinahk20220701.getIndexingandAbstractingStart().substring(0, 4);
        if (StringUtils.isBlank(cinahk20220701.getIndexingandAbstractingStop())) {
            this.timeScope = this.scopeBegin + "至今";
        } else {
            this.scopeEnd = cinahk20220701.getIndexingandAbstractingStop().substring(0, 4);
            this.timeScope = this.scopeBegin + "-" + this.scopeEnd;
        }

        this.createBy = -1;
        this.createDate = LocalDateTime.now();
        this.updateBy = -1;
        this.updateDate = LocalDateTime.now();
    }

    public void createByRsc(Embase20220704 embase20220704, int year, Long recordId) {
        this.collectionId = 1466399812728057858L;
        this.packageId = 1534881876774948875L;
        this.periRecordId = recordId;
        this.sourceType = DraaContent.OrderSource.FROM_DRAA;
        this.year = year;
        this.oa = 0;
        this.dataType = 1;

        this.createBy = -1;
        this.createDate = LocalDateTime.now();
        this.updateBy = -1;
        this.updateDate = LocalDateTime.now();
    }

    public void createByRsc(RscJournal20220706 rsc, int year, Long recordId) {
        this.collectionId = 1466399813789216770L;
        this.packageId = 1468452797343809540L;
        this.periRecordId = recordId;
        this.sourceType = DraaContent.OrderSource.FROM_DRAA;

        String include = rsc.getInclude();
        if (!Strings.isNullOrEmpty(include)) {
            String s = include.replaceAll(" ", "").replace("Present", "至今").replaceAll("Archived:", "");
            this.timeScope = s;
            String[] split = s.split("-");
            this.setScopeBegin(split[0]);
            this.setScopeEnd(split[1]);
        }

        this.url = rsc.getUrl();
        this.year = year;
        this.oa = 0;
        this.dataType = 1;
        this.createBy = -1;
        this.createDate = LocalDateTime.now();
        this.updateBy = -1;
        this.updateDate = LocalDateTime.now();
    }

    public void createByEmbase(Embase20220704 embase20220704, int year, Long recordId) {
        this.collectionId = 1466399812728057858L;
        this.packageId = 1534881876774948875L;
        this.periRecordId = recordId;
        this.sourceType = DraaContent.OrderSource.FROM_DRAA;
        this.year = year;
        this.oa = 0;
        this.dataType = 1;

        this.createBy = -1;
        this.createDate = LocalDateTime.now();
        this.updateBy = -1;
        this.updateDate = LocalDateTime.now();
    }

    public void createByWorld(int oa, int year, Long recordId) {
        this.collectionId = 1466399815248834567L;
        this.packageId = 1509457776212619272L;
        this.periRecordId = recordId;
        this.sourceType = DraaContent.OrderSource.FROM_DRAA;
        this.year = year;
        this.oa = oa;
        this.dataType = 1;

        this.createBy = -1;
        this.createDate = LocalDateTime.now();
        this.updateBy = -1;
        this.updateDate = LocalDateTime.now();
    }
}
